<template>
  <div ref="bar"
       class="charts chart-bar"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
echarts.registerTheme('tdTheme', tdTheme)
export default {
    name: 'ChartBar',
    data () {
        return {
            chart_dom: ''
        }
    },
    props: {
        data: Array
    },
    mounted () {
        this.$nextTick(() => {
            this.init()
        })
    },
    methods: {
        init () {
            this.chart_dom = echarts.init(this.$refs.bar, 'tdTheme')
            let option = {
                title: {
                    text: '对象实例数据量统计',
                    x: 'center',
                    padding: 15
                },
                backgroundColor: '#f0f0f0',
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis:
          {
              type: 'category',
              data: this.data.map(item => {
                  return item.objecName
              }),
              axisTick: {
                  alignWithLabel: true
              },
              // 设置X轴文本的颜色
              axisLabel: {
                  rotate: 20, // 横坐标刻度旋转
                  fontSize: 14
              },
              splitLine: { show: false } // 去除网格线
          },
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        type: 'bar',
                        // markPoint: {
                        //     symbol: 'pin',
                        //     symbolSize: 50,
                        //     data: [
                        //         {type: 'max', name: '最大值'},
                        //         {type: 'min', name: '最小值'}
                        //     ]
                        // },
                        label: {
                            normal: {
                                show: true,
                                formatter: '{c|{c}}',
                                backgroundColor: 'rgb(242,242,242)',
                                borderColor: '#aaa',
                                borderWidth: 1,
                                borderRadius: 4,
                                padding: [4, 5],
                                lineHeight: 26,
                                // shadowBlur: 5,
                                // shadowColor: '#000',
                                // shadowOffsetX: 0,
                                // shadowOffsetY: 1,
                                position: 'top',
                                distance: 20,
                                rich: {
                                    a: {
                                        align: 'center',
                                        color: '#fff',
                                        fontSize: 12,
                                        textShadowBlur: 2,
                                        textShadowColor: '#000',
                                        textShadowOffsetX: 0,
                                        textShadowOffsetY: 1,
                                        textBorderColor: '#333',
                                        textBorderWidth: 2
                                    },
                                    b: {
                                        color: '#333'
                                    },
                                    c: {
                                        color: '#ff8811',
                                        textBorderColor: '#000',
                                        textBorderWidth: 1,
                                        fontSize: 12
                                    }
                                }
                            }
                        },
                        barWidth: '50%',
                        data: this.data.map(item => {
                            return item.totalInstance
                        })
                    }
                ]
            }

            this.chart_dom.setOption(option)
        },
        resize () {
            this.$nextTick(() => {
                this.chart_dom.resize()
            })
        }
    }
}
</script>

<style lang="less">
.chart-bar {
	height: 100%;
}
</style>
